package com.ziguang.redwood.web.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class NewsCenterService {
	private static final Logger log = LoggerFactory.getLogger(NewsCenterService.class);

	public static final NewsCenterService me = new NewsCenterService();
	
	/**
	 * 获取文章列表
	 * @return
	 */
	public List<Record> findNewsList(){
		List<Record> list = Db.find("select *,DATE_FORMAT(create_time,'%Y/%m') as time,DATE_FORMAT(create_time,'%d') as days from rw_news order by create_time");
		for(Record a : list){
			String str = a.get("content");
			str = str.replaceAll("<[^>]+>","");
			str = str.replaceAll("[\r\n]", "");
			str = str.trim();
			int length = str.length();
			log.info(str);
			str = str.substring(0, (length < 60) ? length: 60);
			if(length <= 0){
				a.set("content", "暂无内容");
			}else{
				a.set("content", str);
			}
			
		}
		return list;
	}
	
	/**
	 * 获取文章分页数
	 * @return
	 */
	public Record findCount(){
		Record count = Db.findFirst("select CEIL(count(1)/12) as Ncount from rw_news");
		return count;
	}
	
	/**
	 * 获取文章详细信息
	 * @param id
	 * @return
	 */
	public Record findNewsDetail(String id){
		Record news = Db.findFirst
				("select *,DATE_FORMAT(create_time,'%Y-%m-%d') as time from rw_news where id = ? order by create_time",id);
		Db.update("update rw_news set reading = reading+1 where id = ?",id);
		return news;
	}
	 
}
